function createDownloadButton() {
  const button = document.createElement('button');
  button.className = 'youtube-download-btn';
  button.innerHTML = '下载视频';
  button.onclick = handleDownload;
  
  // 将按钮插入到视频控制栏
  const controls = document.querySelector('.ytp-right-controls');
  if (controls) {
    controls.prepend(button);
  }
}

function handleDownload() {
  const videoUrl = window.location.href;
  chrome.runtime.sendMessage({
    action: 'downloadVideo',
    videoUrl: videoUrl
  });
}

// 监听页面变化，确保在视频页面加载完成后添加下载按钮
const observer = new MutationObserver(() => {
  if (window.location.pathname.includes('/watch')) {
    createDownloadButton();
  }
});

observer.observe(document.body, {
  childList: true,
  subtree: true
});